#region Copyright (c) 2006-2010 Widgetsphere LLC, All Rights Reserved
//--------------------------------------------------------------------- *
//                          Widgetsphere  LLC                           *
//             Copyright (c) 2006-2010 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC   *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT.    *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE,      *
//INC.  THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO       *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion
using System;
using System.Xml;
using Widgetsphere.Generator.Common.GeneratorFramework;
using System.ComponentModel;
using Widgetsphere.Generator.Common.Util;

namespace Widgetsphere.Generator.Models
{
  public enum ReferenceType
  {
    Column = 0,
    Table = 1,
    Relation = 2,
		CustomViewColumn = 3,
    CustomRetrieveRule = 4,
    Parameter = 5,
    CustomStoredProcedureColumn = 6,
		CustomAggregateColumn = 7,
  }

	[
	Designer(typeof(Widgetsphere.Generator.Design.Designers.ReferenceDesigner)),
	DesignTimeVisible(true),
	]
	public class Reference : BaseModelObject
  {
    #region Member Variables

    protected int _ref = 1;
    protected ReferenceType _refType = ReferenceType.Table;

    #endregion

    #region Constructor

    public Reference(IModelObject root)
      : base(root)
    {
    }

    #endregion

    #region Property Implementations

    [Browsable(false)]
    public int Ref
    {
      get { return _ref; }
      set { _ref = value; }
    }

    [Browsable(false)]
    public ReferenceType RefType
    {
      get { return _refType; }
      set { _refType = value; }
    }

    [Browsable(false)]
    public IModelObject Object
    {
      get
      {
        try
        {
          ModelRoot modelRoot = (ModelRoot)this.Root;
          IModelObject retVal = null;
          switch(this.RefType)
          {
            case ReferenceType.Column:
              retVal = modelRoot.Database.Columns[Ref];
              break;
            case ReferenceType.Relation:
              retVal = modelRoot.Database.Relations.GetById(Ref);
              break;
            case ReferenceType.Table:
              retVal = modelRoot.Database.Tables[Ref];
              break;
            case ReferenceType.CustomViewColumn:
              retVal = modelRoot.Database.CustomViewColumns[Ref];
              break;
            case ReferenceType.CustomRetrieveRule:
              retVal = modelRoot.Database.CustomRetrieveRules[Ref];
              break;
            case ReferenceType.Parameter:
              int qq = modelRoot.Database.CustomRetrieveRuleParameters.Count;
              retVal = modelRoot.Database.CustomRetrieveRuleParameters[Ref];
              break;
            case ReferenceType.CustomStoredProcedureColumn:
              retVal = modelRoot.Database.CustomStoredProcedureColumns[Ref];
              break;
            default:
              throw new Exception("Cannot Handle Reference Type");
          }
          return retVal;
        }
        catch(Exception ex)
        {
          throw;
        }
      }
    }

    #endregion

    #region IXMLable Members

    public override void XmlAppend(XmlNode node)
    {
      try
      {
        XmlDocument oDoc = node.OwnerDocument;

        XmlAttribute keyNode = oDoc.CreateAttribute("key");
        keyNode.Value = this.Key.ToString();
        node.Attributes.Append(keyNode);

        XmlAttribute refAttr = oDoc.CreateAttribute("ref");
        refAttr.Value = this.Ref.ToString();
        node.Attributes.Append(refAttr);

        XmlAttribute refType = oDoc.CreateAttribute("refType");
        refType.Value = ((int)this.RefType).ToString();
        node.Attributes.Append(refType);
      }
      catch(Exception ex)
      {
        throw;
      }
    }

    public override void XmlLoad(XmlNode node)
    {
      try
      {
        _key = XmlHelper.GetAttributeValue(node, "key", _key);
        if(node.Attributes.GetNamedItem("refType") != null)
          _refType = (ReferenceType)XmlHelper.GetAttributeValue(node, "refType", (int)_refType);
				if (node.Attributes.GetNamedItem("ref") != null)
					this.Ref = XmlHelper.GetAttributeValue(node, "ref", _ref);

        this.Dirty = false;
      }
      catch(Exception ex)
      {
        throw;
      }
    }

    #endregion

    #region Helpers

    public override string ToString()
    {
      ModelRoot modelRoot = (ModelRoot)this.Root;
      string retval = base.ToString();
      switch(this.RefType)
      {
        case ReferenceType.Column:
          retval = modelRoot.Database.Columns[Ref].ToString();
          break;
        case ReferenceType.Relation:
          retval = modelRoot.Database.Relations.GetById(Ref).ToString();
          break;
        case ReferenceType.Table:
          retval = modelRoot.Database.Tables[Ref].ToString();
          break;
        case ReferenceType.CustomViewColumn:
          retval = modelRoot.Database.CustomViewColumns[Ref].ToString();
          break;
        case ReferenceType.CustomRetrieveRule:
          retval = modelRoot.Database.CustomRetrieveRules[Ref].ToString();
          break;
        case ReferenceType.Parameter:
          retval = modelRoot.Database.CustomRetrieveRuleParameters[Ref].ToString();
          break;
      }
      return retval;
    }

    #endregion

  }

}